package com.bootdo.system.controller;

import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.service.FrontuserService;
import com.bootdo.system.service.JiaoyijiluService;
import com.bootdo.system.service.TuiguanglogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
public class ShequController extends BaseController{

    @Autowired
    private FrontuserService frontuserService;
    @Autowired
    private TuiguanglogService tuiguanglogService;
    @Autowired
    private JiaoyijiluService jiaoyijiluService;

    @GetMapping("/shequ")
    public String shequ(HttpServletRequest request){

        Long userId = getUserId(request);
        //直推
        List<FrontuserDO> userZhituiList = frontuserService.getUserZhituiList(userId);
        request.setAttribute("userZhituiList",setUserZhituiAAndShequ(userZhituiList));

        request.setAttribute("zhituijihuo",getYouxiaonum(userZhituiList));
        request.setAttribute("zhituiwuxiao",getWuxiaonum(userZhituiList));
        //直推盈利
        BigDecimal ztoday = jiaoyijiluService.getTodaytuiguangYingli(userId);
        request.setAttribute("ztoday",ztoday);
        BigDecimal zall = jiaoyijiluService.getAlltuiguangYingli(userId);
        request.setAttribute("zall",zall);


        //社区
        List<FrontuserDO> userShequiList = frontuserService.getUserShequiList(userId, new ArrayList<>());

        request.setAttribute("shequjihuo",getYouxiaonum(userShequiList));
        request.setAttribute("shequwuxiao",getWuxiaonum(userShequiList));
        //社区盈利
        BigDecimal stoday = ShequToday(userShequiList);
        request.setAttribute("stoday",stoday);

        BigDecimal sall = ShequAll(userShequiList);
        request.setAttribute("sall",sall);


        return  "shequ";
    }




    private BigDecimal ShequAll(List<FrontuserDO> userShequiList) {
        BigDecimal result = new BigDecimal(0L);

        for (FrontuserDO u:userShequiList) {

            BigDecimal zall = jiaoyijiluService.getAllYingli(u.getUserid());
            log.info("社区用户直推所有盈利 username="+u.getUserid()+"."+u.getUsername()+" zall="+zall);
            if(zall!=null){

                result = result.add(zall);
            }

        }
        log.info("社区用户直推所有盈利"+result);
        return result;
    }

    private BigDecimal ShequToday(List<FrontuserDO> userShequiList) {

        BigDecimal sum = new BigDecimal(0);

        for (FrontuserDO u:userShequiList) {
            BigDecimal ztoday = jiaoyijiluService.getTodayYingli(u.getUserid());
            if(ztoday==null){
                continue;
            }
            sum = sum.add(ztoday);
        }

        return sum;
    }

    public List<FrontuserDO> setUserZhituiAAndShequ(List<FrontuserDO> userZhituiList){

        for (FrontuserDO u:userZhituiList) {
            List<FrontuserDO> uz1 = frontuserService.getUserZhituiList(u.getUserid());
            u.setZhituinum(uz1==null?0:uz1.size());

            List<FrontuserDO> us1 = frontuserService.getUserShequiList(u.getUserid(), new ArrayList<>());
            u.setShequnum(us1==null?0:us1.size());

        }

        return userZhituiList;
    }


    //得到有效人数
    public int getYouxiaonum(List<FrontuserDO> userZhituiList){

        int num = 0;

        if(userZhituiList==null||userZhituiList.size()==0){
            return num;
        }
        for (FrontuserDO u:userZhituiList) {
            if(u==null||u.getYouxiaotime()==null){
                continue;
            }

            if(u!=null&&u.getYouxiaotime()>0){
                num++;
            }
        }

        return num;

    }
    //得到无效人数
    public int getWuxiaonum(List<FrontuserDO> userZhituiList){

        int num = 0;

        if(userZhituiList==null||userZhituiList.size()==0){
            return num;
        }

        for (FrontuserDO u:userZhituiList) {
            if(u.getYouxiaotime()==null||u.getYouxiaotime()<=0){
                num++;

            }

        }

        return num;

    }



}
